﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using StrategyTest.Common;

namespace StrategyTest.Strategy
{
    /// <summary>
    /// 日内交易策略
    /// </summary>
    public class Test1
    {
        /// <summary>
        /// 账户资金
        /// </summary>
        public double money;

        private double position;

        private int volume;

        public int win;

        public int loss;

        public Test1()
        {
            money       = 20000;
            position    = 0;
            volume      = 5;

            win = 0;
            loss = 0;
        }

        /// <summary>
        /// 获取行情信息
        /// </summary>
        /// <param name="bar">行情Bar</param>
        public void Run(Bar[] bar)
        {
            for (int i = 0; i < bar.Length; i++)
            {
                if (i > 4 && bar[i].OpenTime != DateTime.MinValue)
                {
                    if (position != 0)
                    {
                        //平仓
                        if (bar[i].Low < bar[i - 1].Low)
                        {
                            double result = (bar[i - 1].Low * volume * 10 - position);

                            if (result >= 0)
                                win++;
                            else
                                loss++;

                            money += result;

                            volume = 0;
                            position = 0;
                        }
                        //加仓
                        else if (bar[i].Low > bar[i - 1].Low && volume < 3)
                        {
                            position += bar[i].Open * 10;
                            volume++;
                        }
                    }
                    //开仓
                    else
                    {
                        if (bar[i - 4].Low < bar[i - 3].Low && bar[i - 3].Low < bar[i - 2].Low && bar[i - 2].Low < bar[i - 1].Low)
                        {
                            position += bar[i].Open * 10;
                            volume++;
                        }
                    }
                }
            }
        }
    }
}
